local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseObject())
local CallBackManager = ClientOHOH.load.CallBackManager();

local instStr={
	scaleBtn ="ctrl.Scale.btn",--进入锁定状态
	smallIcon ="ctrl.Scale.smallIcon",
	bigIcon ="ctrl.Scale.bigIcon",

	foundNum = "ctrl.foundNum",

	AstralBtn = "ctrl.pullDown.Astral",
	NoneBtn = "ctrl.pullDown.None",
	NormalBtn = "ctrl.pullDown.Normal",
	RainbowBtn = "ctrl.pullDown.Rainbow",
	ShinyBtn = "ctrl.pullDown.Shiny",
	VoidBtn = "ctrl.pullDown.Void",

	pullDownbtn = "ctrl.pullDown.btn",
	-- testvalue = "ctrl.pullDown.Value",
	--triUp = "ctrl.pullDown.triangle1",
	listFrame= "ctrl.pullDown.listFrame",
	showText= "ctrl.pullDown.showText",

	input= "ctrl.search.input",
 }


function M.new(...)
	return M(...);
end;


function M.ctor(self,ctrl)
	M.super.ctor(self);

    self.ctrl=ctrl
    self.ui=ctrl:InitUiInst2(ctrl.main_inst,instStr)

	self.HadCount=0
	self.MaxPack=0
	self.Select = self.ui.NoneBtn
	self.Select.Visible = false

	--视图大小切换
	CallBackManager:AddRbxConnect(self,self.ui.scaleBtn.Activated, function()
       self:Change()
	end);

	--下拉
	CallBackManager:AddRbxConnect(self,self.ui.pullDownbtn.Activated, function()
		self.ctrl.SearchCancelEvent:Action()
		self.ctrl.CancelShowInfolEvent:Action()
		self:PullChange()
	 end);

	 CallBackManager:AddRbxConnect(self,self.ui.input.Focused, function()
		self.ui.input:GetPropertyChangedSignal("Text"):Connect(function()
			local searchTxt =string.lower(self.ui.input.Text)
			self:SearchTxtChange( searchTxt )
		end)
	 end);
	 self.ctrl.SearchCancelEvent:Connect(function()
		self:SearchTxtChange(nil)
	end)
	 self.ui.input.FocusLost:Connect(function(enterPressed, inputThatCausedFocusLost)
		print("输入框",enterPressed,  inputThatCausedFocusLost)
		if enterPressed then
			print("用户按下了 Enter 键离开了输入框")
		else
			print("用户点击了其他地方离开了输入框")
		end
		-- 检查导致焦点丢失的输入类型
		print("导致焦点丢失的输入类型:", inputThatCausedFocusLost.KeyCode)
	end)

	--  CallBackManager:AddRbxConnect(self,self.ui.input.FocusLost, function(enterPressed,inputThatCausedFocusLost)
	-- 	-- print("____________input lost lost________")
	-- 	print("输入框",enterPressed,  inputThatCausedFocusLost)
	-- 	if enterPressed then
	-- 		print("用户按下了 Enter 键离开了输入框")
	-- 	else
	-- 		print("用户点击了其他地方离开了输入框")
	-- 	end
	-- 	self:SearchTxtChange(nil)
	--  end);



	--下拉列表中的按钮
	--品阶（1：正常:2：闪亮:3：彩虹:4：虚空 5:）
	
	CallBackManager:AddRbxConnect(self,self.ui.NoneBtn.Activated, function()
		if self.Select ~=  self.ui.NoneBtn then
			self.ctrl.PullDownChangeEvent:Action(0)
            self.Select.Visible = true
            self.ui.NoneBtn.Visible = false
            self.Select = self.ui.NoneBtn
			self.ui.showText.Text =  self.Select.Text
			self:PullChange()
        end
	end);
	CallBackManager:AddRbxConnect(self,self.ui.NormalBtn.Activated, function()
		if self.Select ~=  self.ui.NormalBtn then
			self.ctrl.PullDownChangeEvent:Action(1)
            self.Select.Visible = true
            self.ui.NormalBtn.Visible = false
            self.Select = self.ui.NormalBtn
			self.ui.showText.Text =  self.Select.Text
			self:PullChange()
        end
	end);
	CallBackManager:AddRbxConnect(self,self.ui.ShinyBtn.Activated, function()
		if self.Select ~=  self.ui.ShinyBtn then
			self.ctrl.PullDownChangeEvent:Action(2)
            self.Select.Visible = true
            self.ui.ShinyBtn.Visible = false
            self.Select = self.ui.ShinyBtn
			self.ui.showText.Text =  self.Select.Text
			self:PullChange()
        end
	end);
	CallBackManager:AddRbxConnect(self,self.ui.RainbowBtn.Activated, function()
		if self.Select ~=  self.ui.RainbowBtn then
			self.ctrl.PullDownChangeEvent:Action(3)
            self.Select.Visible = true
            self.ui.RainbowBtn.Visible = false
            self.Select = self.ui.RainbowBtn
			self.ui.showText.Text =  self.Select.Text
			self:PullChange()
        end
	end);
	CallBackManager:AddRbxConnect(self,self.ui.VoidBtn.Activated, function()
		if self.Select ~=  self.ui.VoidBtn then
			self.ctrl.PullDownChangeEvent:Action(4)
            self.Select.Visible = true
            self.ui.VoidBtn.Visible = false
            self.Select = self.ui.VoidBtn
			self.ui.showText.Text =  self.Select.Text
			self:PullChange()
        end
	end);
	CallBackManager:AddRbxConnect(self,self.ui.AstralBtn.Activated, function()
		if self.Select ~=  self.ui.AstralBtn then
			 self.ctrl.PullDownChangeEvent:Action(5)
			 self.Select.Visible = true
			 self.ui.AstralBtn.Visible = false
			 self.Select = self.ui.AstralBtn
			 self.ui.showText.Text =  self.Select.Text
			 self:PullChange()
		 end
	 end);

	 self:Init()
end;
function M.Init(self)
	self.ui.smallIcon.Visible = true
	self.ui.bigIcon.Visible = false

	--无法通过ctrl调用三角图标，间接调用得到
	for _,v in ipairs (self.ui.pullDownbtn:GetChildren()) do
		if v.ClassName =="ImageLabel" then
			self.ui.triangle =v
		end
	end
	self.ui.triangle.Rotation = 0

end

function M.dtor(self)
	M.super.dtor(self);
end;

-- function M.UpdateSlotsEquip(self,now,max)

-- end

function M.UpdateStoreInfo(self,max)
	self.MaxPack=max
    self:Update()
end
-- function M.UpdateEquip(self,equip)

-- end

function M.UpdateHad(self,had)
	local count=0
    for k,v in pairs(had) do
        count=count+v
    end
    self.HadCount=count
    self:Update()
end

function M.Update(self)
	
	--self.ctrl:FullText(self.ui.foundNum,{self.HadCount.."/"..self.MaxPack})
 	self.ui.foundNum.Text="已发现:"..self.HadCount.."/"..self.MaxPack
end

function M.Big(self)
    self.ctrl:GetList():Big()
end

function M.Small(self)
    self.ctrl:GetList():Small()
end

function M.Change(self)
	
	if self.ui.smallIcon.Visible then
		self:Small()
    else
		self:Big()
    end
	self.ui.smallIcon.Visible = not self.ui.smallIcon.Visible	
	self.ui.bigIcon.Visible = not self.ui.bigIcon.Visible
	
end

function M.SearchTxtChange(self,txt)
	if txt==nil then
		self.ui.input.Text =""
	end
	local searchString = txt  -- 要匹配的字母或字符串
	self.ctrl.SearchTxtEvent:Action(txt)
-- 遍历数组并匹配包含指定字母或字符串的单词
	-- for index, value in pairs(euipdata) do
	-- 	if string.match(value, searchString) then
	-- 		print("Matched Index:", index, "Matched Value:", value)
	-- 	end
	-- end

end
function M.PullChange(self)
	
	if not self.ui.listFrame.Visible then
		self.ui.listFrame.Visible = true
		self.ui.triangle.Rotation = 180
	else
		self.ui.listFrame.Visible = false
		self.ui.triangle.Rotation = 0
	end
end

return M